/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.amazon.s3;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jets3t.service.S3Service;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Bucket;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;

/**
 *
 * @author Ahmed Salem
 */
public class connect {

    static S3Service s3Service = null;
    private static Object ni;
    private static int i;
    private static Object mac;
    private static String mail;

    public static void main(String[] args) {
        mail = "Ahmed.salem@mufix.org";
        System.out.println("GO to connect");
        connect();
        System.out.println("-------------- List All Buckets -----------------------");
        try {
            for (int i = 0; i < s3Service.listAllBuckets().length; i++) {
                System.out.println("-" + s3Service.listAllBuckets()[i]);
            }
        } catch (S3ServiceException ex) {
            Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("===========================================================");
        //---------- Create buckets --------------------------------------------
        //create_bucket(get_pcname()+(Math.random()*1000000000+1000000000));

        try {
            //======================================================================
            upload_file();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
        } catch (S3ServiceException ex) {
            Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
        }

            S3_display_list();
    }

    public static void connect() {
        String awsAccessKey = "AKIAJU7HCNXAM3LV7CZA";
        String awsSecretKey = "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";
        AWSCredentials awsCredentials =
                new AWSCredentials(awsAccessKey, awsSecretKey);

        try {
            s3Service = new RestS3Service(awsCredentials);
        } catch (S3ServiceException ex) {
            Logger.getLogger(download_files.class.getName()).log(Level.SEVERE, null, ex);
        }

        S3Bucket[] myBuckets = null;
        try {
            myBuckets = s3Service.listAllBuckets();
        } catch (S3ServiceException ex) {
            Logger.getLogger(download_files.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("How many buckets to I have in S3? " + myBuckets.length);

    }

    public static String get_pcname() {
        String computername = "";
        try {
            computername = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException ex) {
            Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(computername);

        return computername;
    }
//===============================================================================
    // public static String getMac() throws java.io.IOException {

    public static String getMac() throws java.io.IOException {
        Process p = Runtime.getRuntime().exec("getmac /fo csv /nh");
        java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
        String line;
        line = in.readLine();
        String[] result = line.split(",");
        return result[0].replace('"', ' ').trim();
    }

    //==========================================================================
    public static void upload_file() throws NoSuchAlgorithmException, IOException, S3ServiceException {



// Create an S3Object based on a file, with Content-Length set automatically and
// Content-Type set based on the file's extension (using the Mimetypes utility class)
        File fileData = new File("D://New.txt");
        S3Object fileObject = new S3Object(fileData);

        // Create an empty object with a key/name, and print the object's details.
        S3Object object = new S3Object("object");
        System.out.println("S3Object before upload: " + object);

// Upload the object to our test bucket in S3.
        object = s3Service.putObject("salem-backet", fileObject);
        System.out.println("Bucket Name ==>" + object.getBucketName());
        System.out.println("Object MDhash Hex ==>" + object.getMd5HashAsHex());
// Print the details about the uploaded object, which contains more information.
        System.out.println("S3Object after upload: " + object);


    }

    //================================== List Bucket -=============================
    public static void S3_display_list() {
        // List all your buckets.
        S3Bucket[] buckets = null;
        try {
            buckets = s3Service.listAllBuckets();
        } catch (S3ServiceException ex) {
            Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
        }

// List the object contents of each bucket.
        for (int b = 0; b < buckets.length; b++) {
            System.out.println("Bucket '" + buckets[b].getName() + "' contains:");

            // List the objects in this bucket.
            S3Object[] objects = null;
            try {
                objects = s3Service.listObjects(buckets[b]);
            } catch (S3ServiceException ex) {
                Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
            }

            // Print out each object's key and size.
            for (int o = 0; o < objects.length; o++) {
                System.out.println(" " + objects[o].getKey() + " (" + objects[o].getContentLength() + " bytes)");
            }
        }

    }

    //==================================================================================
    
}

